#include "settingspage.h"
#include "manager.h"

settingspage::settingspage(QWidget *parent)
    : QWidget(parent)
{
	ui.setupUi(this);
	
	// set papers
	ui.listWidget_3->addItems(Manager::papers);
	// set formats
	ui.treeWidget->setColumnWidth(0,80);
	ui.treeWidget->setColumnWidth(1,30);
	ui.treeWidget->setColumnWidth(2,80);
	QList<QTreeWidgetItem*> items;
	QStringList list;
	for(int i=0; i<Manager::formats.size(); ++i)
	{
		list<<Manager::formats[i].size<<Manager::formats[i].cut_part<<Manager::formats[i].press_sheet;
		QTreeWidgetItem *it = new QTreeWidgetItem((QTreeWidget*)0, list);
		items.append(it);		
		list.clear();
	}
	ui.treeWidget->insertTopLevelItems(0, items); 
	// set printers
	for(int p = 0; p < Manager::printers.size(); ++p) {
		ui.tableWidget->insertRow(p);
		ui.tableWidget->setRowHeight(p, 20);
		ui.tableWidget->setItem(p,0,new QTableWidgetItem(Manager::printers[p].name));
		ui.tableWidget->setItem(p,1,new QTableWidgetItem(Manager::printers[p].format));
	}
	
	ui.lineEdit->setText(Manager::manager_name);
	ui.lineEdit_2->setText(Manager::manager_city);
	ui.lineEdit_4->setText(Manager::manager_press);
	ui.lineEdit_15->setText(Manager::manager_address);
	ui.lineEdit_13->setText(Manager::manager_fone);
	ui.lineEdit_14->setText(Manager::manager_fax);
	ui.lineEdit_10->setText(Manager::manager_mail);
	
	connect(ui.pushButton_11, SIGNAL(clicked()), this, SLOT(AddNewPaper()));
	connect(ui.pushButton_12, SIGNAL(clicked()), this, SLOT(DeleteSelPaper()));
	connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(AddNewFormat()));
	connect(ui.pushButton_14, SIGNAL(clicked()), this, SLOT(DeleteSelFormat()));
	connect(ui.pushButton_15, SIGNAL(clicked()), this, SLOT(SavePrinter()));
	connect(ui.pushButton_13, SIGNAL(clicked()), this, SLOT(DeleteSelPrinter()));
	connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(SavePersonalInfo()));
	connect(ui.tableWidget, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), this, SLOT(SelectPrinter(QTableWidgetItem*)));
}

settingspage::~settingspage()
{
	QDomDocument doc;	
	QDomElement root = doc.createElement("PaperList");
	doc.appendChild(root);
					
	for(int ind = 0; ind < Manager::papers.size(); ++ind)
	{
		QDomElement paper = doc.createElement("paper");
		QDomElement name_cell = doc.createElement("name");
										
		root.appendChild(paper);
		paper.appendChild(name_cell);
			
		QDomText name = doc.createTextNode(Manager::papers[ind]);
		name_cell.appendChild(name);
	}

	QString p =  Manager::data_path.path();
	p += QString("ListPapers.xml");
	QFile file (p);
	file.open(QFile::WriteOnly | QFile::Text);
	QTextStream out2(&file);	
	QDomNode xmlNode = doc.createProcessingInstruction("xml", "version = \"1.0\" encoding = \"Windows-1251\"");
	doc.insertBefore(xmlNode, doc.firstChild());
	out2.setCodec("Windows-1251");		
	doc.save(out2, 4);
	
	doc.clear();
	root = doc.createElement("FormatList");
	doc.appendChild(root);
					
	for(int ind = 0; ind < Manager::formats.size(); ++ind)
	{
		QDomElement format_cell = doc.createElement("format");
		QDomElement size_cell = doc.createElement("size");
		QDomElement cut_part_cell = doc.createElement("cut_part");
		QDomElement press_sheet_cell = doc.createElement("press_sheet");
										
		root.appendChild(format_cell);
		format_cell.appendChild(size_cell);
		format_cell.appendChild(cut_part_cell);
		format_cell.appendChild(press_sheet_cell);
			
		QDomText size = doc.createTextNode(Manager::formats[ind].size);
		size_cell.appendChild(size);
						
		QDomText cut_part = doc.createTextNode(Manager::formats[ind].cut_part);
		cut_part_cell.appendChild(cut_part);
						
		QDomText press_sheet = doc.createTextNode(Manager::formats[ind].press_sheet);
		press_sheet_cell.appendChild(press_sheet);
	}

	p =  Manager::data_path.path();
	p += QString("ListFormat.xml");
	QFile file2 (p);
	file2.open(QFile::WriteOnly | QFile::Text);
	QTextStream out3(&file2);	
	doc.insertBefore(xmlNode, doc.firstChild());
	out3.setCodec("Windows-1251");		
	doc.save(out3, 4);
	
	doc.clear();
	root = doc.createElement("PrinterList");
	doc.appendChild(root);
	for(int ind = 0; ind < Manager::printers.size(); ++ind)
	{
		QDomElement printer = doc.createElement("printer");
		QDomElement name_cell = doc.createElement("name");
		QDomElement format_cell = doc.createElement("format");
											
		root.appendChild(printer);
		printer.appendChild(name_cell);
		printer.appendChild(format_cell);
				
		QDomText name = doc.createTextNode(Manager::printers[ind].name);
		QDomText format = doc.createTextNode(Manager::printers[ind].format);
		name_cell.appendChild(name);
		format_cell.appendChild(format);
	}

	p =  Manager::data_path.path();
	p += QString("ListPrinters.xml");
	QFile file3 (p);
	file3.open(QFile::WriteOnly | QFile::Text);
	QTextStream out4(&file3);	
	doc.insertBefore(xmlNode, doc.firstChild());
	out4.setCodec("Windows-1251");		
	doc.save(out4, 4);	
}

void settingspage::SavePersonalInfo()
{
	Manager::manager_name = ui.lineEdit->text();
	Manager::manager_city = ui.lineEdit_2->text();
	Manager::manager_press = ui.lineEdit_4->text();
	Manager::manager_address = ui.lineEdit_15->text();
	Manager::manager_fone = ui.lineEdit_13->text();
	Manager::manager_fax = ui.lineEdit_14->text();
	Manager::manager_mail = ui.lineEdit_10->text();
	
	QDomDocument doc;	
	QDomElement	root = doc.createElement("settings");
	
	doc.appendChild(root);
	QDomElement manager_name_cell = doc.createElement("manager_name");
	QDomElement manager_city_cell = doc.createElement("manager_city");
	QDomElement press_cell = doc.createElement("manager_press");
	QDomElement address_cell = doc.createElement("manager_address");
	QDomElement fone_cell = doc.createElement("manager_fone");
	QDomElement fax_cell = doc.createElement("manager_fax");
	QDomElement mail_cell = doc.createElement("manager_mail");
	QDomElement path_orders_cell = doc.createElement("path_orders");
	QDomElement path_data_cell = doc.createElement("path_data");
	root.appendChild(manager_name_cell);
	root.appendChild(manager_city_cell);
	root.appendChild(press_cell);
	root.appendChild(address_cell);
	root.appendChild(fone_cell);
	root.appendChild(fax_cell);
	root.appendChild(mail_cell);
	root.appendChild(path_orders_cell);
	root.appendChild(path_data_cell);
	QDomText manager_name = doc.createTextNode(ui.lineEdit->text());
	manager_name_cell.appendChild(manager_name);
	QDomText manager_city = doc.createTextNode(ui.lineEdit_2->text());
	manager_city_cell.appendChild(manager_city);
	QDomText press = doc.createTextNode(ui.lineEdit_4->text());
	press_cell.appendChild(press);
	QDomText address = doc.createTextNode(ui.lineEdit_15->text());
	address_cell.appendChild(address);
	QDomText fone = doc.createTextNode(ui.lineEdit_13->text());
	fone_cell.appendChild(fone);
	QDomText fax = doc.createTextNode(ui.lineEdit_14->text());
	fax_cell.appendChild(fax);
	QDomText mail = doc.createTextNode(ui.lineEdit_10->text());
	mail_cell.appendChild(mail);
	
	QDomText path_orders = doc.createTextNode(Manager::order_path.path());
	path_orders_cell.appendChild(path_orders);
	QDomText path_data = doc.createTextNode(Manager::data_path.path());
	path_data_cell.appendChild(path_data);
			
	QFile file ("settings.xml");
	file.open(QFile::WriteOnly | QFile::Text);
	QTextStream out(&file);	
	QDomNode xmlNode = doc.createProcessingInstruction("xml", "version = \"1.0\" encoding = \"Windows-1251\"");
	doc.insertBefore(xmlNode, doc.firstChild());
	out.setCodec("Windows-1251");		
	doc.save(out, 4);
}

int settingspage::AddNewPaper()
{
	QString new_paper = ui.lineEdit_6->text();
	if(new_paper == "")
		return 1;
	if(Manager::papers.contains(new_paper))
		return 1;
	Manager::papers << new_paper;
	Manager::papers.sort();
	ui.listWidget_3->clear();
	ui.listWidget_3->addItems(Manager::papers);
	return 1;
}

int settingspage::DeleteSelPaper()
{
	QListWidgetItem *cur_item = ui.listWidget_3->currentItem();
	if(!cur_item) return 1;
	QString paper_name = cur_item->text();
	Manager::papers.removeAt(Manager::papers.indexOf(paper_name));
	ui.listWidget_3->clear();
	ui.listWidget_3->addItems(Manager::papers);
	return 1;
}

int settingspage::AddNewFormat()
{
	if(ui.lineEdit_7->text() == "" || ui.lineEdit_8->text() == ""
		|| ui.lineEdit_11->text() == "" || ui.lineEdit_12->text() == "")
		return 1;
	CPaper temp;
	temp.size = ui.lineEdit_7->text();
	temp.size += " x ";
	temp.size += ui.lineEdit_8->text();
	temp.cut_part = ui.comboBox->currentText();
	temp.press_sheet = ui.lineEdit_11->text();
	temp.press_sheet += " x ";
	temp.press_sheet += ui.lineEdit_12->text();
	for(int i = 0; i < Manager::formats.size(); ++i)
	{
		if(Manager::formats[i] == temp)
			return 1;
	}
	Manager::formats.push_back(temp);
	QStringList list;
	list<<temp.size<<temp.cut_part<<temp.press_sheet;
	QTreeWidgetItem *it = new QTreeWidgetItem((QTreeWidget*)0, list);
	ui.treeWidget->insertTopLevelItem(0, it); 
	return 1;
}

int settingspage::DeleteSelFormat()
{
	QTreeWidgetItem *cur_item = ui.treeWidget->currentItem();
	if(!cur_item) return 1;
	CPaper temp;
	temp.size = cur_item->text(1);
	temp.cut_part = cur_item->text(2);
	temp.press_sheet = cur_item->text(3);
	for(int i = 0; i < Manager::formats.size(); ++i)
	{
		if(Manager::formats[i] == temp)
		{
			Manager::formats.removeAt(i);
			ui.treeWidget->clear();
			QList<QTreeWidgetItem*> items;
			QStringList list;
			for(int i=0; i<Manager::formats.size(); ++i)
			{
				list<<Manager::formats[i].size<<Manager::formats[i].cut_part<<Manager::formats[i].press_sheet;
				QTreeWidgetItem *it = new QTreeWidgetItem((QTreeWidget*)0, list);
				items.append(it);		
				list.clear();
			}
			ui.treeWidget->insertTopLevelItems(0, items);
			return 1;
		}
	}	 
	return 1;
}

int settingspage::SavePrinter()
{
	QString printer_name = ui.lineEdit_9->text();
	if(printer_name == "")
		return 1;
	QString printer_width = ui.lineEdit_16->text();
	QString printer_height = ui.lineEdit_17->text();
	SPrinter temp;
	temp.name 	= printer_name;
	temp.format = printer_width + " x " + printer_height;
	
	for(int p = 0; p < Manager::printers.size(); ++p) {
		if(temp.name == Manager::printers[p].name){
			Manager::printers[p].format = temp.format;
			for(int r = 0; r < ui.tableWidget->rowCount(); ++r) {
				QTableWidgetItem* item = ui.tableWidget->item(r, 0); 
				QString name = item->text();
				if(name == temp.name) {
					QTableWidgetItem* it = ui.tableWidget->item(r, 1);
					it->setText(temp.format);
					return 1;
				}
			}
			return 1;
		}
	}
	
	int count = ui.tableWidget->rowCount();
	ui.tableWidget->insertRow(count);
	ui.tableWidget->setRowHeight(count, 20);
	ui.tableWidget->setItem(count,0,new QTableWidgetItem(temp.name));
	ui.tableWidget->setItem(count,1,new QTableWidgetItem(temp.format));
	Manager::printers.push_back(temp);
	return 1;
}

int settingspage::DeleteSelPrinter()
{
	QTableWidgetItem *cur_item = ui.tableWidget->currentItem();
	if(!cur_item) return 1;
	int row = cur_item->row();
	QTableWidgetItem* item = ui.tableWidget->item(row, 0); 
	QString printer_name = item->text();
	ui.tableWidget->removeRow(row);
	for(int p = 0; p < Manager::printers.size(); ++p){
		if(printer_name == Manager::printers[p].name){
			Manager::printers.removeAt(p);
			return 1;
		}
	}
	return 1;
}

void settingspage::SelectPrinter(QTableWidgetItem* it) 
{
	int row = it->row();
	QString name = ui.tableWidget->item(row, 0)->text();
	QString format = ui.tableWidget->item(row, 1)->text();
	QStringList wh = format.split(" x ");
	ui.lineEdit_9->setText(name);
	ui.lineEdit_16->setText(wh[0]);
	ui.lineEdit_17->setText(wh[1]);
}